/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define INCLUDE_FROM_OBJC_C

#include "data_impl.h"
#include "texture_impl.h"
#include "PVRTCLoader.h"
#include "Texture2D.h"
#include <cassert>

namespace Graph2D
{
	namespace Implementation
	{
		namespace Texture
		{
			void* load(const std::string& filename, const bool compressed)
			{
				void* result = NULL;

				NSString* file = [NSString stringWithUTF8String:filename.c_str()];
				NSString* path = [DataController getResoucePathWithString:file];
				if(compressed)
				{
					result = new PVRTCLoader([path UTF8String]);
				}
				else
				{
					NSData* data = [[NSData alloc] initWithContentsOfFile:path];
					if(data)
					{
						UIImage* image = [[UIImage alloc] initWithData:data];
						if(image)
						{
							result = [[Texture2D alloc] initWithImage:image];

							[image release];
						}

						[data release];
					}
				}

				return result;
			}

			void unload(void* self, const bool compressed)
			{
				if(self)
				{
					if(compressed)
					{
						PVRTCLoader* instance = reinterpret_cast<PVRTCLoader*>(self);
						delete instance;
					}
					else
					{
						Texture2D* instance = reinterpret_cast<Texture2D*>(self);
						[instance release];
					}
				}
			}

			GLuint getName(void* self, const bool compressed)
			{
				assert(self);
				if(compressed)
				{
					PVRTCLoader* instance = reinterpret_cast<PVRTCLoader*>(self);
					return instance->getName();
				}
				else
				{
					Texture2D* instance = reinterpret_cast<Texture2D*>(self);
					return instance.name;
				}
			}

			Vector2 getSize(void* self, const bool compressed)
			{
				assert(self);
				if(compressed)
				{
					PVRTCLoader* instance = reinterpret_cast<PVRTCLoader*>(self);
					return Vector2(instance->getWidth(), instance->getHeight());
				}
				else
				{
					Texture2D* instance = reinterpret_cast<Texture2D*>(self);
					return Vector2(instance.contentSize.width, instance.contentSize.height);
				}
			}

			Vector2 getTextureSize(void* self, const bool compressed)
			{
				assert(self);
				if(compressed)
				{
					PVRTCLoader* instance = reinterpret_cast<PVRTCLoader*>(self);
					return Vector2(instance->getWidth(), instance->getHeight());
				}
				else
				{
					Texture2D* instance = reinterpret_cast<Texture2D*>(self);
					return Vector2(static_cast<float>(instance.pixelsWide), static_cast<float>(instance.pixelsHigh));
				}
			}

			Vector2 getMaxSize(void* self, const bool compressed)
			{
				assert(self);
				if(compressed)
				{
					PVRTCLoader* instance = reinterpret_cast<PVRTCLoader*>(self);
					return Vector2(instance->getWidth(), instance->getHeight());
				}
				else
				{
					Texture2D* instance = reinterpret_cast<Texture2D*>(self);
					return Vector2(instance.maxS, instance.maxT);
				}
			}
		}
	}
}

